package com.tencent.cloud.iov.block;

import android.view.View;
import android.view.ViewGroup;

import com.tencent.cloud.iov.action.IActionListener;

import java.util.ArrayList;
import java.util.List;

/**
 * IBlockLayout 组合类
 *
 * @author showxiao
 * @date 2018/4/18
 */

public final class CompositeBlockLayout implements IBlockLayout {

    private List<IBlockLayout> mBlockLayoutList = new ArrayList<>();
    private CompositeBlockView mCompositeBlockView = new CompositeBlockView();

    public CompositeBlockLayout() {
        this(null);
    }

    public CompositeBlockLayout(List<IBlockLayout> list) {
        if (list != null && !list.isEmpty()) {
            mBlockLayoutList.addAll(list);
        }
    }

    @Override
    public CompositeBlockView onCreateView(ViewGroup parent) {
        for (IBlockLayout blockLayout : mBlockLayoutList) {
            View view = blockLayout.onCreateView(parent);
            mCompositeBlockView.addView(view);
        }

        return mCompositeBlockView;
    }

    @Override
    public void onBindView(Object item, int position) {
        for (IBlockLayout blockLayout : mBlockLayoutList) {
            blockLayout.onBindView(item, position);
        }
    }

    @Override
    public void onBindAction(Object item, int position, IActionListener actionListener) {
        for (IBlockLayout blockLayout : mBlockLayoutList) {
            blockLayout.onBindAction(item, position, actionListener);
        }
    }

    public void addBlockLayout(IBlockLayout blockLayout) {
        if (mBlockLayoutList.contains(blockLayout)) {
            mBlockLayoutList.remove(blockLayout);
        }

        mBlockLayoutList.add(blockLayout);
    }

    public void removeBlockLayout(IBlockLayout blockLayout) {
        mBlockLayoutList.remove(blockLayout);
    }
}
